const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');
var app = getApp();

Page({
  data: {
    userInfo: {},
    userInfo: {
      nickName: '点击登录',
      mobile: null,
      member: null,
      sellerId: null,
      avatarUrl: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'
    }
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log(app.globalData)
  },
  onReady: function () {

  },
  onShow: function () {
    this.onLoad();
    let userInfo = wx.getStorageSync('userInfo');
    let token = wx.getStorageSync('token');

    //获取用户的登录信息
    if (app.globalData.hasLogin) {
      let userInfo = wx.getStorageSync('userInfo');
      console.log('userInfo:'+userInfo);
      this.setData({
        userInfo: userInfo,
      });
    }
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },

  getUserInfo: function (e) {
    console.log('getUserInfo==' + e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  goLogin() {
    if (!app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },

  bindGetUserInfo(e) {
    let userInfo = wx.getStorageSync('userInfo');
    let token = wx.getStorageSync('token');
    if (userInfo && token) {
      return;
    }
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      user.loginByWeixin(e.detail).then(res => {
        this.setData({
          userInfo: res.obj.userinfo
        });
        app.globalData.userInfo = res.obj.userinfo;
        app.globalData.token = res.obj.token;
      }).catch((err) => {
        console.log(err)
      });
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告通知',
        content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
        success: function (res) {
          if (res.confirm) {
            wx.openSetting({
              success: (res) => {
                if (res.authSetting["scope.userInfo"]) { ////如果用户重新同意了授权登录
                  user.loginByWeixin(e.detail).then(res => {
                    this.setData({
                      userInfo: res.data.userInfo
                    });
                    app.globalData.userInfo = res.data.userInfo;
                    app.globalData.token = res.data.token;
                  }).catch((err) => {
                    console.log(err)
                  });
                }
              }
            })
          }
        }
      });
    }
  },

  bindMobile(){
    console.info('去绑定手机号码');
    wx.navigateTo({
      url: "/pages/mine/bindMobile/bindMobile"
    });
  },

  changeMobile(){
    console.info('修改手机号码');
    wx.navigateTo({
      url: "/pages/mine/bindMobile/bindMobile?mobile=" + this.data.userInfo.mobile
    });
  },
  bindDistrutor(){
    wx.navigateTo({
      url: '/pages/mine/bindDistrutor/bindDistrutor'
    })
  },
  isLogin: function (_url) {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: _url
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },

  goOrder() {
    var url = "/pages/ucenter/order/order?showType=0"
    this.isLogin(url)
  },

  packge() {
       var url = "/pages/mine/package/package"
       this.isLogin(url)
  },

  goadress() {
    var url = "/pages/ucenter/address/address"
    this.isLogin(url)
  },
  // 热疗仪订单
  goDeviceOrder() {
    var url = "/pages/ucenter/deviceOrder/deviceOrder?isUse=0"
    this.isLogin(url)
  },
  goDistribution(){
    var url = "/pages/ucenter/distribution/distribution?isUse=0";
    this.isLogin(url);
  }
})